import math
class ad9361PLL:

	"""
	AD9361 RF and BB PLL Synthesizer User Guide
	page 13-18
	rx reg 0x23A[3:0]  0x239[3:0]  0x242[2:0]  0x242[4:3]  0x238[6:3]  0x251[3:0]  0x23B[5:0]  0x23E[7:4]  0x23E[3:0]  0x23F[7:4]  0x23F[3:0]  0x240[3:0]
	tx reg 0x27A[3:0]  0x279[3:0]  0x282[2:0]  0x282[4:3]  0x278[6:3]  0x291[3:0]  0x27B[5:0]  0x27E[7:4]  0x27E[3:0]  0x27F[7:4]  0x27F[3:0]  0x280[3:0]
	for FDD:
		VCO CAL COUNT = 1024
		RX: 0x249[3:2]=2'b11
		TX: 0x289[3:2]=2'b11
	Fix reg set
	SPIWrite 261,00  // Set Rx LO Power mode
	SPIWrite 248,0B  // Enable Rx VCO LDO
	SPIWrite 246,02  // Power Down Cal Tcf (RX)
	SPIWrite 243,0D // Set Prescaler Bias
	SPIWrite 245,00  // Set VCO Cal Ref Tcf[2:0] (RX)
	SPIWrite 250,70 // Set VCO Varactor Ref Tcf[2:0] and VCO Varactor Ref[3:0] (RX)
	SPIWrite 239,C1 // Init ALC Value (RX)
	SPIWrite 23B,80  // Ensure MSB Set (Set to 1 from Reg Map) (RX)
	SPIWrite 23D,00 // Clear Half VCO cal clock setting
	"""

	_tab_rx_reg = [
			  ""
			, ""
			, ""
			, ""
			, ""
			, ""
			, "0x23A[3:0]"
			, "0x239[3:0]"
			, "0x242[2:0]"
			, "0x242[4:3]"
			, "0x238[6:3]"
			, "0x251[3:0]"
			, "0x23B[5:0]"
			, "0x23E[7:4]"
			, "0x23E[3:0]"
			, "0x23F[7:4]"
			, "0x23F[3:0]"
			, "0x240[3:0]"
		]
	_tab_tx_reg = [
			  ""
			, ""
			, ""
			, ""
			, ""
			, ""
			, "0x27A[3:0]"
			, "0x279[3:0]"
			, "0x282[2:0]"
			, "0x282[4:3]"
			, "0x278[6:3]"
			, "0x291[3:0]"
			, "0x27B[5:0]"
			, "0x27E[7:4]"
			, "0x27E[3:0]"
			, "0x27F[7:4]"
			, "0x27F[3:0]"
			, "0x280[3:0]"
		]
	_tab_head = [
			  "Band"  
			, "Reference Frequency"
			, "Loop Bandwidth"
			, "Index"
			, "VCO Frequency"
			, "VCO Kv"
			, "VCO Output Level [3:0]"
			, "VCO Varactor [3:0]"
			, "VCO Bias Ref [2:0]"
			, "VCO Bias Tcf [1:0]"
			, "VCO Cal Offset [3:0]"
			, "VCO Varactor Reference [3:0]"
			, "Charge Pump Current [5:0]"
			, "Loop Filter C2 [3:0]"
			, "Loop Filter C1 [3:0]"
			, "Loop Filter R1 [3:0]"
			, "Loop Filter C3 [3:0]"
			, "Loop Filter R3 [3:0]"
		]
	_tab_40M = """
  0  4.000E+07  2.500E+05   1  1.2605E+10  1.518E+08  10  0  4  0  15   8   8  12  3  14  15  11
  8  4.000E+07  2.500E+05   2  1.2245E+10  1.373E+08  10  0  4  0  15   8   9  12  3  14  15  11
 16  4.000E+07  2.500E+05   3  1.1906E+10  1.249E+08  10  0  4  0  15   8   9  12  3  14  15  11
 24  4.000E+07  2.500E+05   4  1.1588E+10  1.142E+08  10  0  4  0  15   8  10  12  3  14  15  11
 32  4.000E+07  2.500E+05   5  1.1288E+10  1.049E+08  10  0  4  0  15   8  11  12  3  14  15  11
 40  4.000E+07  2.500E+05   6  1.1007E+10  9.674E+07  10  0  4  0  15   8  11  12  3  14  15  11
 48  4.000E+07  2.500E+05   7  1.0742E+10  8.957E+07  10  0  4  0  14   8  12  12  3  14  15  11
 56  4.000E+07  2.500E+05   8  1.0492E+10  8.323E+07  10  0  5  1  14   9  13  12  3  14  15  11
 64  4.000E+07  2.500E+05   9  1.0258E+10  7.758E+07  10  0  5  1  14   9  13  12  3  14  15  11
 72  4.000E+07  2.500E+05  10  1.0036E+10  7.254E+07  10  0  5  1  14   9  14  12  3  14  15  11
 80  4.000E+07  2.500E+05  11  9.8278E+09  6.801E+07  10  0  5  1  14   9  15  12  3  14  15  11
 88  4.000E+07  2.500E+05  12  9.6311E+09  6.393E+07  10  0  5  1  14   9  15  12  3  14  15  11
 96  4.000E+07  2.500E+05  13  9.4453E+09  6.024E+07  10  0  5  1  14   9  16  12  3  14  15  11
104  4.000E+07  2.500E+05  14  9.2698E+09  5.689E+07  10  0  5  1  14   9  17  12  3  14  15  11
112  4.000E+07  2.500E+05  15  9.1036E+09  5.384E+07  10  0  5  1  14   9  17  12  3  14  15  11
120  4.000E+07  2.500E+05  16  8.9463E+09  5.105E+07  10  0  5  1  14   9  18  12  3  14  15  11
128  4.000E+07  2.500E+05  17  8.7970E+09  6.748E+07  10  1  6  1  15  11  13  12  3  14  15  11
136  4.000E+07  2.500E+05  18  8.6553E+09  6.422E+07  10  1  6  1  15  11  14  12  3  14  15  11
144  4.000E+07  2.500E+05  19  8.5206E+09  6.121E+07  10  1  6  1  15  11  14  12  3  14  15  11
152  4.000E+07  2.500E+05  20  8.3923E+09  5.843E+07  10  1  6  1  15  11  15  12  3  14  15  11
160  4.000E+07  2.500E+05  21  8.2699E+09  5.586E+07  10  1  6  1  15  11  15  12  3  14  15  11
168  4.000E+07  2.500E+05  22  8.1531E+09  5.348E+07  10  1  6  1  15  11  16  12  3  14  15  11
176  4.000E+07  2.500E+05  23  8.0414E+09  5.126E+07  10  1  6  1  15  11  16  12  3  14  15  11
184  4.000E+07  2.500E+05  24  7.9344E+09  4.919E+07  10  1  6  1  15  11  17  12  3  14  15  11
192  4.000E+07  2.500E+05  25  7.8318E+09  4.726E+07  10  1  6  1  15  11  17  12  3  14  15  11
200  4.000E+07  2.500E+05  26  7.7332E+09  4.546E+07  10  1  6  1  15  11  17  12  3  14  15  11
208  4.000E+07  2.500E+05  27  7.6384E+09  4.376E+07  10  1  6  1  15  11  18  12  3  14  15  11
216  4.000E+07  2.500E+05  28  7.5471E+09  4.217E+07  10  1  6  1  15  11  18  12  3  14  15  11
224  4.000E+07  2.500E+05  29  7.4590E+09  4.068E+07  10  1  6  1  15  11  19  12  3  14  15  11
232  4.000E+07  2.500E+05  30  7.3740E+09  3.927E+07  10  1  7  2  15  12  19  12  3  14  15  11
240  4.000E+07  2.500E+05  31  7.2919E+09  3.794E+07  10  1  7  2  15  12  20  12  3  14  15  11
248  4.000E+07  2.500E+05  32  7.2124E+09  3.668E+07  10  1  7  2  15  12  20  12  3  14  15  11
256  4.000E+07  2.500E+05  33  7.1355E+09  3.549E+07  10  1  7  2  15  14  21  12  3  14  15  11
264  4.000E+07  2.500E+05  34  7.0610E+09  3.437E+07  10  1  7  2  15  14  21  12  3  14  15  11
272  4.000E+07  2.500E+05  35  6.9887E+09  3.330E+07  10  1  7  2  15  14  22  12  3  14  15  11
280  4.000E+07  2.500E+05  36  6.9186E+09  3.228E+07  10  1  7  2  15  14  22  12  3  14  15  11
288  4.000E+07  2.500E+05  37  6.8506E+09  3.132E+07  10  1  7  2  15  14  23  12  3  14  15  11
296  4.000E+07  2.500E+05  38  6.7846E+09  3.041E+07  10  1  7  2  15  14  23  12  3  14  15  11
304  4.000E+07  2.500E+05  39  6.7205E+09  2.953E+07  10  1  7  2  15  14  24  12  3  14  15  11
312  4.000E+07  2.500E+05  40  6.6582E+09  2.870E+07  10  1  7  2  15  14  24  12  3  14  15  11
320  4.000E+07  2.500E+05  41  6.5978E+09  2.791E+07  10  1  7  2  15  14  25  12  3  14  15  11
328  4.000E+07  2.500E+05  42  6.5392E+09  2.716E+07  10  1  7  2  15  14  25  12  3  14  15  11
336  4.000E+07  2.500E+05  43  6.4823E+09  2.643E+07  10  1  7  2  15  14  26  12  3  14  15  11
344  4.000E+07  2.500E+05  44  6.4270E+09  2.575E+07  10  1  7  2  15  14  26  12  3  14  15  11
352  4.000E+07  2.500E+05  45  6.3734E+09  3.920E+07  10  3  7  3  15  12  17  12  3  14  15  11
360  4.000E+07  2.500E+05  46  6.3214E+09  3.821E+07  10  3  7  3  15  12  17  12  3  14  15  11
368  4.000E+07  2.500E+05  47  6.2709E+09  3.727E+07  10  3  7  3  15  12  17  12  3  14  15  11
376  4.000E+07  2.500E+05  48  6.2220E+09  3.637E+07  10  3  7  3  15  12  18  12  3  14  15  11
384  4.000E+07  2.500E+05  49  6.1745E+09  3.550E+07  10  3  7  3  15  12  18  12  3  14  15  11
392  4.000E+07  2.500E+05  50  6.1284E+09  3.468E+07  10  3  7  3  15  12  18  12  3  14  15  11
400  4.000E+07  2.500E+05  51  6.0836E+09  3.388E+07  10  3  7  3  15  12  18  12  3  14  15  11
408  4.000E+07  2.500E+05  52  6.0401E+09  3.312E+07  10  3  7  3  15  12  19  12  3  14  15  11
416  4.000E+07  2.500E+05  53  5.9977E+09  3.238E+07  10  3  7  3  15  12  19  12  3  14  15  11
"""
	_tab_60M = """
  0  6.000E+07  3.000E+05   1  1.2605E+10  1.518E+08  10  0  4  0  15   8  10  15  4  13  15  10
  8  6.000E+07  3.000E+05   2  1.2245E+10  1.373E+08  10  0  4  0  15   8  11  15  4  13  15  10
 16  6.000E+07  3.000E+05   3  1.1906E+10  1.249E+08  10  0  4  0  15   8  11  15  4  13  15  10
 24  6.000E+07  3.000E+05   4  1.1588E+10  1.142E+08  10  0  4  0  15   8  12  15  4  13  15  10
 32  6.000E+07  3.000E+05   5  1.1288E+10  1.049E+08  10  0  4  0  15   8  13  15  4  13  15  10
 40  6.000E+07  3.000E+05   6  1.1007E+10  9.674E+07  10  0  4  0  14   8  14  15  4  13  15  10
 48  6.000E+07  3.000E+05   7  1.0742E+10  8.957E+07  10  0  4  0  14   8  15  15  4  13  15  10
 56  6.000E+07  3.000E+05   8  1.0492E+10  8.323E+07  10  0  5  1  14   9  15  15  4  13  15  10
 64  6.000E+07  3.000E+05   9  1.0258E+10  7.758E+07  10  0  5  1  14   9  16  15  4  13  15  10
 72  6.000E+07  3.000E+05  10  1.0036E+10  7.254E+07  10  0  5  1  14   9  17  15  4  13  15  10
 80  6.000E+07  3.000E+05  11  9.8278E+09  6.801E+07  10  0  5  1  14   9  18  15  4  13  15  10
 88  6.000E+07  3.000E+05  12  9.6311E+09  6.393E+07  10  0  5  1  14   9  19  15  4  13  15  10
 96  6.000E+07  3.000E+05  13  9.4453E+09  6.024E+07  10  0  5  1  14   9  19  15  4  13  15  10
104  6.000E+07  3.000E+05  14  9.2698E+09  5.689E+07  10  0  5  1  14   9  20  15  4  13  15  10
112  6.000E+07  3.000E+05  15  9.1036E+09  5.384E+07  10  0  5  1  13   9  21  15  4  13  15  10
120  6.000E+07  3.000E+05  16  8.9463E+09  5.105E+07  10  0  5  1  13   9  22  15  4  13  15  10
128  6.000E+07  3.000E+05  17  8.7970E+09  6.748E+07  10  1  6  1  15  11  16  15  4  13  15  10
136  6.000E+07  3.000E+05  18  8.6553E+09  6.422E+07  10  1  6  1  15  11  17  15  4  13  15  10
144  6.000E+07  3.000E+05  19  8.5206E+09  6.121E+07  10  1  6  1  15  11  17  15  4  13  15  10
152  6.000E+07  3.000E+05  20  8.3923E+09  5.843E+07  10  1  6  1  15  11  18  15  4  13  15  10
160  6.000E+07  3.000E+05  21  8.2699E+09  5.586E+07  10  1  6  1  15  11  18  15  4  13  15  10
168  6.000E+07  3.000E+05  22  8.1531E+09  5.348E+07  10  1  6  1  15  11  19  15  4  13  15  10
176  6.000E+07  3.000E+05  23  8.0414E+09  5.126E+07  10  1  6  1  15  11  19  15  4  13  15  10
184  6.000E+07  3.000E+05  24  7.9344E+09  4.919E+07  10  1  6  1  15  11  20  15  4  13  15  10
192  6.000E+07  3.000E+05  25  7.8318E+09  4.726E+07  10  1  6  1  15  11  21  15  4  13  15  10
200  6.000E+07  3.000E+05  26  7.7332E+09  4.546E+07  10  1  6  1  15  11  21  15  4  13  15  10
208  6.000E+07  3.000E+05  27  7.6384E+09  4.376E+07  10  1  6  1  15  11  22  15  4  13  15  10
216  6.000E+07  3.000E+05  28  7.5471E+09  4.217E+07  10  1  6  1  15  11  22  15  4  13  15  10
224  6.000E+07  3.000E+05  29  7.4590E+09  4.068E+07  10  1  6  1  15  11  23  15  4  13  15  10
232  6.000E+07  3.000E+05  30  7.3740E+09  3.927E+07  10  1  7  2  15  12  23  15  4  13  15  10
240  6.000E+07  3.000E+05  31  7.2919E+09  3.794E+07  10  1  7  2  15  12  24  15  4  13  15  10
248  6.000E+07  3.000E+05  32  7.2124E+09  3.668E+07  10  1  7  2  15  12  25  15  4  13  15  10
256  6.000E+07  3.000E+05  33  7.1355E+09  3.549E+07  10  1  7  2  15  14  25  15  4  13  15  10
264  6.000E+07  3.000E+05  34  7.0610E+09  3.437E+07  10  1  7  2  15  14  26  15  4  13  15  10
272  6.000E+07  3.000E+05  35  6.9887E+09  3.330E+07  10  1  7  2  15  14  26  15  4  13  15  10
280  6.000E+07  3.000E+05  36  6.9186E+09  3.228E+07  10  1  7  2  15  14  27  15  4  13  15  10
288  6.000E+07  3.000E+05  37  6.8506E+09  3.132E+07  10  1  7  2  15  14  27  15  4  13  15  10
296  6.000E+07  3.000E+05  38  6.7846E+09  3.041E+07  10  1  7  2  15  14  28  15  4  13  15  10
304  6.000E+07  3.000E+05  39  6.7205E+09  2.953E+07  10  1  7  2  15  14  29  15  4  13  15  10
312  6.000E+07  3.000E+05  40  6.6582E+09  2.870E+07  10  1  7  2  15  14  29  15  4  13  15  10
320  6.000E+07  3.000E+05  41  6.5978E+09  2.791E+07  10  1  7  2  15  14  30  15  4  13  15  10
328  6.000E+07  3.000E+05  42  6.5392E+09  2.716E+07  10  1  7  2  15  14  30  15  4  13  15  10
336  6.000E+07  3.000E+05  43  6.4823E+09  2.643E+07  10  1  7  2  15  14  31  15  4  13  15  10
344  6.000E+07  3.000E+05  44  6.4270E+09  2.575E+07  10  1  7  2  15  14  32  15  4  13  15  10
352  6.000E+07  3.000E+05  45  6.3734E+09  3.920E+07  10  3  7  3  15  12  20  15  4  13  15  10
360  6.000E+07  3.000E+05  46  6.3214E+09  3.821E+07  10  3  7  3  15  12  21  15  4  13  15  10
368  6.000E+07  3.000E+05  47  6.2709E+09  3.727E+07  10  3  7  3  15  12  21  15  4  13  15  10
376  6.000E+07  3.000E+05  48  6.2220E+09  3.637E+07  10  3  7  3  15  12  21  15  4  13  15  10
384  6.000E+07  3.000E+05  49  6.1745E+09  3.550E+07  10  3  7  3  15  12  22  15  4  13  15  10
392  6.000E+07  3.000E+05  50  6.1284E+09  3.468E+07  10  3  7  3  15  12  22  15  4  13  15  10
400  6.000E+07  3.000E+05  51  6.0836E+09  3.388E+07  10  3  7  3  15  12  22  15  4  13  15  10
408  6.000E+07  3.000E+05  52  6.0401E+09  3.312E+07  10  3  7  3  15  12  23  15  4  13  15  10
416  6.000E+07  3.000E+05  53  5.9977E+09  3.238E+07  10  3  7  3  15  12  23  15  4  13  15  10
"""
	_tab_80M = """
  0  8.000E+07  3.500E+05   1  1.2605E+10  1.518E+08  10  0  4  0  15   8   8  13  4  13  15  9
  8  8.000E+07  3.500E+05   2  1.2245E+10  1.373E+08  10  0  4  0  15   8   9  13  4  13  15  9
 16  8.000E+07  3.500E+05   3  1.1906E+10  1.249E+08  10  0  4  0  15   8  10  13  4  13  15  9
 24  8.000E+07  3.500E+05   4  1.1588E+10  1.142E+08  10  0  4  0  15   8  11  13  4  13  15  9
 32  8.000E+07  3.500E+05   5  1.1288E+10  1.049E+08  10  0  4  0  15   8  11  13  4  13  15  9
 40  8.000E+07  3.500E+05   6  1.1007E+10  9.674E+07  10  0  4  0  14   8  12  13  4  13  15  9
 48  8.000E+07  3.500E+05   7  1.0742E+10  8.957E+07  10  0  4  0  14   8  13  13  4  13  15  9
 56  8.000E+07  3.500E+05   8  1.0492E+10  8.323E+07  10  0  5  1  14   9  13  13  4  13  15  9
 64  8.000E+07  3.500E+05   9  1.0258E+10  7.758E+07  10  0  5  1  14   9  14  13  4  13  15  9
 72  8.000E+07  3.500E+05  10  1.0036E+10  7.254E+07  10  0  5  1  14   9  15  13  4  13  15  9
 80  8.000E+07  3.500E+05  11  9.8278E+09  6.801E+07  10  0  5  1  14   9  15  13  4  13  15  9
 88  8.000E+07  3.500E+05  12  9.6311E+09  6.393E+07  10  0  5  1  13   9  16  13  4  13  15  9
 96  8.000E+07  3.500E+05  13  9.4453E+09  6.024E+07  10  0  5  1  13   9  17  13  4  13  15  9
104  8.000E+07  3.500E+05  14  9.2698E+09  5.689E+07  10  0  5  1  13   9  18  13  4  13  15  9
112  8.000E+07  3.500E+05  15  9.1036E+09  5.384E+07  10  0  5  1  13   9  18  13  4  13  15  9
120  8.000E+07  3.500E+05  16  8.9463E+09  5.105E+07  10  0  5  1  13   9  19  13  4  13  15  9
128  8.000E+07  3.500E+05  17  8.7970E+09  6.748E+07  10  1  6  1  15  11  14  13  4  13  15  9
136  8.000E+07  3.500E+05  18  8.6553E+09  6.422E+07  10  1  6  1  15  11  14  13  4  13  15  9
144  8.000E+07  3.500E+05  19  8.5206E+09  6.121E+07  10  1  6  1  15  11  15  13  4  13  15  9
152  8.000E+07  3.500E+05  20  8.3923E+09  5.843E+07  10  1  6  1  15  11  15  13  4  13  15  9
160  8.000E+07  3.500E+05  21  8.2699E+09  5.586E+07  10  1  6  1  15  11  16  13  4  13  15  9
168  8.000E+07  3.500E+05  22  8.1531E+09  5.348E+07  10  1  6  1  15  11  16  13  4  13  15  9
176  8.000E+07  3.500E+05  23  8.0414E+09  5.126E+07  10  1  6  1  15  11  17  13  4  13  15  9
184  8.000E+07  3.500E+05  24  7.9344E+09  4.919E+07  10  1  6  1  15  11  17  13  4  13  15  9
192  8.000E+07  3.500E+05  25  7.8318E+09  4.726E+07  10  1  6  1  15  11  18  13  4  13  15  9
200  8.000E+07  3.500E+05  26  7.7332E+09  4.546E+07  10  1  6  1  15  11  18  13  4  13  15  9
208  8.000E+07  3.500E+05  27  7.6384E+09  4.376E+07  10  1  6  1  15  11  19  13  4  13  15  9
216  8.000E+07  3.500E+05  28  7.5471E+09  4.217E+07  10  1  6  1  15  11  19  13  4  13  15  9
224  8.000E+07  3.500E+05  29  7.4590E+09  4.068E+07  10  1  6  1  15  11  20  13  4  13  15  9
232  8.000E+07  3.500E+05  30  7.3740E+09  3.927E+07  10  1  7  2  15  12  20  13  4  13  15  9
240  8.000E+07  3.500E+05  31  7.2919E+09  3.794E+07  10  1  7  2  15  12  21  13  4  13  15  9
248  8.000E+07  3.500E+05  32  7.2124E+09  3.668E+07  10  1  7  2  15  12  21  13  4  13  15  9
256  8.000E+07  3.500E+05  33  7.1355E+09  3.549E+07  10  1  7  2  15  14  22  13  4  13  15  9
264  8.000E+07  3.500E+05  34  7.0610E+09  3.437E+07  10  1  7  2  15  14  22  13  4  13  15  9
272  8.000E+07  3.500E+05  35  6.9887E+09  3.330E+07  10  1  7  2  15  14  23  13  4  13  15  9
280  8.000E+07  3.500E+05  36  6.9186E+09  3.228E+07  10  1  7  2  15  14  23  13  4  13  15  9
288  8.000E+07  3.500E+05  37  6.8506E+09  3.132E+07  10  1  7  2  15  14  24  13  4  13  15  9
296  8.000E+07  3.500E+05  38  6.7846E+09  3.041E+07  10  1  7  2  15  14  24  13  4  13  15  9
304  8.000E+07  3.500E+05  39  6.7205E+09  2.953E+07  10  1  7  2  15  14  25  13  4  13  15  9
312  8.000E+07  3.500E+05  40  6.6582E+09  2.870E+07  10  1  7  2  15  14  25  13  4  13  15  9
320  8.000E+07  3.500E+05  41  6.5978E+09  2.791E+07  10  1  7  2  15  14  26  13  4  13  15  9
328  8.000E+07  3.500E+05  42  6.5392E+09  2.716E+07  10  1  7  2  15  14  26  13  4  13  15  9
336  8.000E+07  3.500E+05  43  6.4823E+09  2.643E+07  10  1  7  2  15  14  27  13  4  13  15  9
344  8.000E+07  3.500E+05  44  6.4270E+09  2.575E+07  10  1  7  2  15  14  27  13  4  13  15  9
352  8.000E+07  3.500E+05  45  6.3734E+09  3.920E+07  10  3  7  3  15  12  18  13  4  13  15  9
360  8.000E+07  3.500E+05  46  6.3214E+09  3.821E+07  10  3  7  3  15  12  18  13  4  13  15  9
368  8.000E+07  3.500E+05  47  6.2709E+09  3.727E+07  10  3  7  3  15  12  18  13  4  13  15  9
376  8.000E+07  3.500E+05  48  6.2220E+09  3.637E+07  10  3  7  3  15  12  19  13  4  13  15  9
384  8.000E+07  3.500E+05  49  6.1745E+09  3.550E+07  10  3  7  3  15  12  19  13  4  13  15  9
392  8.000E+07  3.500E+05  50  6.1284E+09  3.468E+07  10  3  7  3  15  12  19  13  4  13  15  9
400  8.000E+07  3.500E+05  51  6.0836E+09  3.388E+07  10  3  7  3  15  12  19  13  4  13  15  9
408  8.000E+07  3.500E+05  52  6.0401E+09  3.312E+07  10  3  7  3  15  12  20  13  4  13  15  9
416  8.000E+07  3.500E+05  53  5.9977E+09  3.238E+07  10  3  7  3  15  12  20  13  4  13  15  9
"""
	def str2Reg(slef,s):
		d0 = s.split('[')
		d1 = d0[1].split(':')
		regaddr = int(d0[0],16)
		s = int(d1[1][:-1])
		e = int(d1[0])
		l = e-s+1
		return {'addr':regaddr,'start':s,'length':l}
	
	def str2rlist(self,s):
		d0 = s.split()
		r = [int(a) for a in d0[6:]]
		return r

	def tab2list(self,s):
		lines = s.splitlines()
		ret = []
		for l in lines:
			if len(l)==0:
				continue
			ret.append(self.str2rlist(l))
		return ret

	def tab2vcof(self,s):
		lines = s.splitlines()
		ret = []
		#print lines
		for l in lines:
			if len(l)==0:
				continue
			d0 = l.split()
			#print d0
			ret.append(float(d0[4]))
		return ret

	def buildTab(self):
		f = self.ref/self.div
		if f<50e6:
			self.table = self.tab2list(ad9361PLL._tab_40M)
		elif f<70e6:
			self.table = self.tab2list(ad9361PLL._tab_60M)
		else:
			self.table = self.tab2list(ad9361PLL._tab_80M)
		self.regs = [ self.str2Reg(s) for s in ad9361PLL._tab_rx_reg[6:]]
		self.vcofs = self.tab2vcof(ad9361PLL._tab_40M)
		
	def f2inx(self,f):
		for i in range(len(self.vcofs)):
			if self.vcofs[i]<f:
				return i
		return len(self.vcofs)

	def initReg(self,regs):
		ret = {}
		for r in regs:
			ret[r["addr"]]=0
		return ret

	def setReg(self,d):
		rs = self.regs
		r_dict = {}
		for i in range(len(d)):
			v = d[i]<<rs[i]["start"]
			m = (1<<rs[i]["length"])-1
			m <<= rs[i]["start"]
			flag = 0
			for (rr,rv) in self.Fixed:
				if rr==rs[i]["addr"]:
					rv &=(~m)
					rv |= v
					r_dict[rr]=rv
					flag = 1
					break
			if flag==0:
				rr = rs[i]["addr"]
				if rr in r_dict:
					r_dict[rr] |= v
				else:
					r_dict[rr] = v
		ret = []
		for rr in r_dict:
			ret.append((rr,r_dict[rr]))
		return ret


			#print hex(rs[i]["addr"])+"[%d:%d]"%(rs[i]["start"]+rs[i]["length"]-1,rs[i]["start"]),ad9361PLL._tab_head[i+6],hex(d[i])
			#print r[-1]

	def Reg(self,vcof):
		inx = self.f2inx(vcof)
		print "inx",inx
		return self.setReg(self.table[inx])
		

	def RFFreqCalc(self,f):
		VCO_Divider = math.floor(math.log(12e9/f)/math.log(2.))
		F_RFPLL = (2**VCO_Divider)*f
		n = F_RFPLL/(self.ref/self.div)
		N_integer = int(math.floor(n))
		N_fractional = int(round(8388593.*(n-N_integer)))
		return (int(VCO_Divider)-1,N_integer,N_fractional,F_RFPLL)

	def Set_freq(self,f):
		Base=0x231
		(D,I,F,V)=self.RFFreqCalc(f)
		ret = self.Reg(V)
		W = []
		W.append(I&0xff)
		W.append((I>>8)&0x3)
		W.append(F&0xff)
		F >>= 8
		W.append(F&0xff)
		F >>= 8
		W.append(F&0x7f)
		for i in range(1,5):
			ret.append((Base+i,W[i]))
		ret.append((Base,W[0]))
		return D,ret

	def __init__(self,ref):
		self.ref = ref
		self.Fixed = [    (0x261,0x00)
						, (0x248,0x0B)
						, (0x246,0x02)
						, (0x243,0x0D)
						, (0x245,0x00)
						, (0x250,0x70)
						, (0x239,0xC1)
						, (0x23B,0x80)
						, (0x23D,0x00)
						, (0x23A,0x40)
						, (0x249,0x8E) # for FDD
						]

		if self.ref<40e6:
			self.div = 0.5
		else:
			self.div = 1.

		self.buildTab()

	def rx2tx(self,rx):
		ret = []
		for (r,v) in rx:
			ret.append((r+0x40,v))
		return ret

	def setRxTx(self,rx_f,tx_f):
		Dr,retr = self.Set_freq(rx_f)
		Dt,rett = self.Set_freq(tx_f)
		rett = self.rx2tx(rett)
		ret = retr+rett
		ret.append((5,(Dt<<4)|Dr))
		return ret

def main():
	uut = ad9361PLL(25e6)
	
	ret = uut.setRxTx(940e6,900e6)
	for (r,d) in ret:
		print hex(r),hex(d)

if __name__ == '__main__':
	main()



		